local screenW, screenH = guiGetScreenSize()
local sx, sy = guiGetScreenSize()
local w, h = (sx/1680),(sy/1050)
local tick = getTickCount()
opcjaWybrana = 1
alpha1 = 255
alpha2 = 255

loadstring(exports.dgs:dgsImportFunction())()
font = dgsCreateFont( ":cmta-pobieraniezasobow/OSB.ttf", (11.5)*w )
font2 = dgsCreateFont( ":cmta-pobieraniezasobow/OSB.ttf", 25*w )


local motorki_hehe = {[463] = true,[462] = true,[461] = true,[581] = true,[448] = true,[468] = true,[471] = true,[521] = true,[522] = true,[523] = true,[586] = true} 

function stworzListe()
opcje = {}
local veh = getPedOccupiedVehicle(localPlayer)
	if veh then
	--	local rh_lvl = getElementData(veh, "rh:up") or 0
		--local naped = getElementData(veh, "naped:lvl") or "WYBIERZ NAPĘD"
		local data = getElementData(veh, "vehicle:data")
		if not getVehicleEngineState(veh) then
			table.insert(opcje, {"Odpal silnik", 1})
		else
			table.insert(opcje, {"Zgaś silnik", 2})
		end
		  

		if getVehicleOverrideLights(veh) ~= 2 then
			table.insert(opcje, {"Włącz światła", 3})
		else
			table.insert(opcje, {"Zgaś światła", 4})
		end

		if motorki_hehe[getElementModel(veh)] then
		if isElementFrozen(veh) then
			table.insert(opcje, {"Podnieś stopkę", 7})
		else
			table.insert(opcje, {"Opuść stopkę", 8})
		end
		else
		if isElementFrozen(veh) then
			table.insert(opcje, {"Spuść ręczny", 7})
		else
			table.insert(opcje, {"Zaciągnij ręczny", 8})
			end
		end

        if not motorki_hehe[getElementModel(veh)] then
		if isVehicleLocked(veh) then
			table.insert(opcje, {"Otwórz pojazd", 5})
		else
			table.insert(opcje, {"Zamknij pojazd", 6})
		end
	    end

        if getVehicleDoorOpenRatio(veh,0) == 0 then
            table.insert(opcje, {"Otwórz maskę", 9})
		else
			table.insert(opcje, {"Zamknij maskę", 10})
		end

        if getVehicleDoorOpenRatio(veh,1) == 0 then
            table.insert(opcje, {"Otwórz bagażnik", 11})
		else
			table.insert(opcje, {"Zamknij bagażnik", 12})
		end		

		    table.insert(opcje, {"Wysadź pasażerów", 13})
  
		if getElementData(veh, "vehicle:data") and getElementData(veh, "vehicle:data").asr then
		if getElementData(veh, "vehicle:asr") == "wlaczone" then
			table.insert(opcje, {"Wyłącz system ESP", 14})
		else
			table.insert(opcje, {"Włącz system ESP", 15})
		end
		end	
				
		if getElementData(veh, "vehicle:data") and getElementData(veh, "vehicle:data").sterringlock == 1 then
          	table.insert(opcje, {"Kąt skrętu kół: "..(getVehicleHandlingProperty(veh,"steeringLock") or 0).."", 16})
		end

 		 end
		if #opcje > 0 then
		if not show then
		bindKey("arrow_u", "down", onSuwakZmien)
		bindKey("arrow_d", "down", onSuwakZmien)
		bindKey("arrow_r", "down", editSLUp)
		bindKey("arrow_l", "down", editSLDown)
		bindKey("space", "down", onSuwakWybierz)
		showChat(false)
		setElementData(localPlayer, "notshowhud", true)
		setPlayerHudComponentVisible("radar", false)
		tick = getTickCount()
		setPlayerHudComponentVisible("radar", false)
		showChat(false)
		addEventHandler("onClientRender", getRootElement(), dxDrawPanelInterakcji)
		show = true
		end
	end
end


function ostatniPojazd()
local veh = getElementData(localPlayer, "lastVeh")
	if veh then
		if isElement(veh) then
		local driver = getElementData(veh, "lastDriver")
			if driver == localPlayer then
			local dx, dy, dz = getElementPosition(localPlayer)
			local vx, vy, vz = getElementPosition(veh)
				if 10 >= getDistanceBetweenPoints3D(dx, dy, dz, vx, vy, vz) then
				saveV = veh
				return veh
				end
			end
		end
	end
end


rowery = {

[481] = true

}

function wlaczPanelInterakcji()
local veh = getPedOccupiedVehicle(localPlayer)
if getPlayerSerial(localPlayer) ~= "C8CE5CAFE9EBB28B1174121E7643B783" then return end
	if veh then
		if rowery[getElementModel(veh)] then return end
		if getVehicleController(veh) == localPlayer then   
		stworzListe()
		end
	else
	local veh = ostatniPojazd()
		if veh then
		if rowery[getElementModel(veh)] then return end
		stworzListe()
		end
	end
end

function wylaczPanelInterakcji()
	if show then
	show = false		
	unbindKey("arrow_u", "down", onSuwakZmien)
	unbindKey("arrow_d", "down", onSuwakZmien)
	unbindKey("arrow_r", "down", editSLUp)
	unbindKey("arrow_l", "down", editSLDown)

	unbindKey("space", "down", onSuwakWybierz)
	showChat(true)
	setElementData(localPlayer, "notshowhud", false)
	setPlayerHudComponentVisible("radar", true)
	opcjaWybrana = 1
	removeEventHandler("onClientRender", getRootElement(), dxDrawPanelInterakcji)
	saveV = nil
	end
end

bindKey("lshift", "down", wlaczPanelInterakcji)
bindKey("lshift", "up", wylaczPanelInterakcji)



function dxDrawPanelInterakcji()
	if not isPedInVehicle(localPlayer) then return end
	alpha12 = interpolateBetween(0, 0, 0, 235, 0, 0, (getTickCount()-tick)/100, "Linear")
	alpha13 = interpolateBetween(0, 0, 0, 255, 0, 0, (getTickCount()-tick)/100, "Linear")
    dxDrawRectangle(0*w, 0*h, 2000*w, 2000*h, tocolor(15, 15, 15, alpha12), false)


	for i,v in pairs(opcje) do
		
		if i == opcjaWybrana-1 then
		dxDrawText(v[1], 0*w, 432*h, 1680*w, 585*h, tocolor(255, 255, 255, 120), 1, font, "center", "center", false, false, false, false, false)
	    end
		if i == opcjaWybrana then
        dxDrawText(v[1], 0*w, 532*h, 1680*w, 585*h, tocolor(255,160,122, alpha13), 1, font2, "center", "center", false, false, false, false, false)
		else
		dxDrawText(v[1], 0*w, 532*h, 1680*w, 585*h, tocolor(255, 255, 255, 0), 1, font2, "center", "center", false, false, false, false, false)
		end
		if i == opcjaWybrana+1 then
			dxDrawText(v[1], 0*w, 632*h, 1680*w, 585*h, tocolor(255, 255, 255, 120), 1, font, "center", "center", false, false, false, false, false)
		end
	end


	if getKeyState("arrow_l") then
	alpha1 = 155
	else
	alpha1 = 255
	end

	if getKeyState("arrow_r") then
	alpha2 = 155
	else
	alpha2 = 255
	end

	dxDrawRectangle ( 0*w, 485*h, 1680*w, 150*h, tocolor ( 0, 0, 0, 150 ) )

	if opcje[opcjaWybrana][2] == 16 then
        dxDrawText("#808080Użyj strzałek (#ff0000↑ ↓#808080) aby zmienić opcję\nUżyj strzałek (#ff0000← →#808080) do edycji ustawień", 0*w, 300*h, 1680*w, 532*h, tocolor(255, 140, 0, alpha13), 1, font2, "center", "center", false, false, false, true, false)
        dxDrawText("Aby edytować ustawienia musisz zatrzymać swój pojazd.", 0*w, 700*h, 1680*w, 608*h, tocolor(255, 0, 0, alpha13), 1, font, "center", "center", false, false, false, false, false)
 		dxDrawImage(50*w, 535*h, 51*w, 51*h, "arrow.png", 180, 0, 0, tocolor(255,255,255, alpha1), true)
        dxDrawImage(1570*w, 535*h, 51*w, 51*h, "arrow.png", 0, 0, 0, tocolor(255,255,255, alpha2), true)
    else
        dxDrawText("#808080Użyj strzałek (#ff0000↑ ↓#808080) aby zmienić opcję", 0*w, 350*h, 1680*w, 532*h, tocolor(255, 140, 0, alpha13), 1, font2, "center", "center", false, false, false, true, false)	
	end



	if #opcje == 1 then
	opcjaWybrana = 1
	end

stworzListe()
end


function onSuwakZmien(k,ks)
	if k  == "arrow_d" then --na dol
	local sound = playSound("sound2.mp3")
	setSoundVolume(sound,0.4)
	opcjaWybrana = opcjaWybrana - 1
		if opcjaWybrana == 0 then
		opcjaWybrana = #opcje
		end
	else
	opcjaWybrana = opcjaWybrana + 1
		if opcjaWybrana == #opcje + 1 then
		opcjaWybrana = 1
		end
	end
end

function onSuwakZmien(k,ks)
	local sound = playSound("sound2.mp3")
	setSoundVolume(sound,0.4)
	if k  == "arrow_u" then --na dol
	opcjaWybrana = opcjaWybrana - 1
		if opcjaWybrana == 0 then
		opcjaWybrana = #opcje
		end
	else
	opcjaWybrana = opcjaWybrana + 1
		if opcjaWybrana == #opcje + 1 then
		opcjaWybrana = 1
		end
	end
end



function editSLDown(k,ks)
	local veh = getPedOccupiedVehicle(localPlayer)
	if getElementSpeed(veh, 1) > 5 then return end
	if getVehicleHandlingProperty(veh,"steeringLock") <= 20 then return end
	if opcje[opcjaWybrana][1] == "Kąt skrętu kół: "..(getVehicleHandlingProperty(veh,"steeringLock") or 0).."" then
	if k  == "arrow_l" then
	timer = timer or 0 
    timerb = getTickCount()
    if timer > timerb then return end
    timer = getTickCount()+1000
	triggerServerEvent("SL:zmniejsz", localPlayer, localPlayer)
	playSound("sound.mp3")
	end
end
end

function editSLUp(k,ks)
	local veh = getPedOccupiedVehicle(localPlayer)
	if getElementSpeed(veh, 1) > 5 then return end
	if getVehicleHandlingProperty(veh,"steeringLock") >= 50 then return end
	if opcje[opcjaWybrana][1] == "Kąt skrętu kół: "..(getVehicleHandlingProperty(veh,"steeringLock") or 0).."" then
	if k  == "arrow_r" then
	timer = timer or 0 
	timerb = getTickCount()
	if timer > timerb then return end
	timer = getTickCount()+1000
	triggerServerEvent("SL:zwieksz", localPlayer, localPlayer)
	playSound("sound.mp3")
	end
end
end

function onSuwakWybierz()
	local veh = getPedOccupiedVehicle(localPlayer)
	
	if opcje[opcjaWybrana][2] == 17 or opcje[opcjaWybrana][2] == 18 then
	if getElementSpeed(veh, 1) > 5 then return end
	end

	if #opcje == 1 then
	triggerServerEvent("onInterakcjeWybierzAkcje", localPlayer, false, saveV)
	return
	end

	if opcjaWybrana then
	local id = opcje[opcjaWybrana][2]
		if id then
			if id == 7 and getElementSpeed(veh, 1) > 5 then return end
			if id == 8 and getElementSpeed(veh, 1) > 5 then return end
		triggerServerEvent("onInterakcjeWybierzAkcje", localPlayer, id, saveV)
		end
	end
end


function getVehicleHandlingProperty ( element, property )
    if isElement ( element ) and getElementType ( element ) == "vehicle" and type ( property ) == "string" then
        local handlingTable = getVehicleHandling ( element ) 
        local value = handlingTable[property] 
 
        if value then
            return value
        end
    end
 
    return false
end


function getElementSpeed(theElement, unit)
    assert(isElement(theElement), "Bad argument 1 @ getElementSpeed (element expected, got " .. type(theElement) .. ")")
    local elementType = getElementType(theElement)
    assert(elementType == "player" or elementType == "ped" or elementType == "object" or elementType == "vehicle" or elementType == "projectile", "Invalid element type @ getElementSpeed (player/ped/object/vehicle/projectile expected, got " .. elementType .. ")")
    assert((unit == nil or type(unit) == "string" or type(unit) == "number") and (unit == nil or (tonumber(unit) and (tonumber(unit) == 0 or tonumber(unit) == 1 or tonumber(unit) == 2)) or unit == "m/s" or unit == "km/h" or unit == "mph"), "Bad argument 2 @ getElementSpeed (invalid speed unit)")
    unit = unit == nil and 0 or ((not tonumber(unit)) and unit or tonumber(unit))
    local mult = (unit == 0 or unit == "m/s") and 50 or ((unit == 1 or unit == "km/h") and 180 or 111.84681456)
    return (Vector3(getElementVelocity(theElement)) * mult).length
end



